package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.SysCompeScoringSettingDetail;
import com.ruoyi.system.service.SysCompeScoringSettingDetailService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author 柳
 * @Date 2021/3/4
 * @Description TODO
 * 评分项信息操作
 */
@RestController
@RequestMapping("/system/scoring")
public class SysCompeScoringSettingDetailController {

    @Autowired
    private SysCompeScoringSettingDetailService compeScoringSettingDetailService;


    @Autowired
    private RedisTemplate<String,String> redisTemplate;




    /**
     * 根据大赛id查询评分项
     * @param epid
     * @return
     */
    @GetMapping("/queryCompeScoringSettingDetail/{epid}")
    @ApiOperation("根据大赛id查询评分项")
    public AjaxResult queryCompeScoringSettingDetail(@PathVariable Integer epid) {
        AjaxResult result = AjaxResult.success(compeScoringSettingDetailService.queryCompeScoringSettingDetail(epid));
        return result;
    }

    @GetMapping("/queryCompeScoringWork")
    @ApiOperation("根据大赛id查询评分项")
    public AjaxResult queryCompeScoringWork(Integer epid, String workId) {
        AjaxResult result = null;
        List<SysCompeScoringSettingDetail> sysCompeScoringSettingDetails = compeScoringSettingDetailService.queryCompeScoringSettingDetail(epid);
        //如果评分项不为空
        if (sysCompeScoringSettingDetails != null && sysCompeScoringSettingDetails.size() > 0) {
            String key = epid+"_"+workId; //缓存关键字
            String val = redisTemplate.opsForValue().get(key);
            if (!StringUtils.isEmpty(val)){
                result =  AjaxResult.error("该作品其他老师正在评审，请评审其他作品或稍后再试！");
            } else {
                //redis中标识该作品在评审中， 有效时间5分钟
                redisTemplate.opsForValue().set(key, "正在评审中",5, TimeUnit.MINUTES);
                result = AjaxResult.success(sysCompeScoringSettingDetails);
            }
        } else {
            result =  AjaxResult.error("该赛事未设置评分项，请先联系管理员设置评分项!!!");
        }
        return result;
    }


    /**
     * 评分项设置(添加评分项信息)
     */
    @PostMapping("/saveCompeScoringSettingDetail/{epid}")
    @ApiOperation("评分项设置(添加评分项信息)")
    public AjaxResult saveCompeScoringSettingDetail(@RequestBody List<SysCompeScoringSettingDetail> list,@PathVariable Integer epid) {
        //删除原有的评分项信息
        compeScoringSettingDetailService.deleteCompeScoringSettingDetailByEpid(epid);
        //批量添加评分项信息
        for (SysCompeScoringSettingDetail compeScoringSettingDetail : list) {
            compeScoringSettingDetail.setEpid(epid);
            compeScoringSettingDetailService.saveCompeScoringSettingDetail(compeScoringSettingDetail);
        }
        return AjaxResult.success();
    }
}
